package com.example.meitu.api.common;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class UserContextUtils {

    public static List<Integer> getCompetitorBrandIds(Integer brandId){
        List<Integer> result = new ArrayList<>();
        result.add(brandId);
        List<UserBrandsContext> brands = UserLoginContextHolder.get().getBrands();
        Map<Integer, List<UserBrandsContext>> map = brands.stream().collect(Collectors.groupingBy(UserBrandsContext::getBrandId));
        List<UserBrandsContext> competitorBrandList = map.get(brandId);
        if (CollectionUtil.isNotEmpty(competitorBrandList)) {
            for (UserBrandsContext userBrandsContext : competitorBrandList) {
                List<Map> list = userBrandsContext.getCompetitor();
                for (Map map1 : list) {
                    Integer competitorBrandId = Integer.parseInt((String) map1.get("competitorBrandId"));
                    result.add(competitorBrandId);
                }
            }
        }
        return result;
    }

    public static Map getCompetitorBrand(Integer brandId){
        Map<Integer,String> result = new HashMap<Integer, String>();
        List<UserBrandsContext> brands = UserLoginContextHolder.get().getBrands();
        Map<Integer, List<UserBrandsContext>> map = brands.stream().collect(Collectors.groupingBy(UserBrandsContext::getBrandId));
        List<UserBrandsContext> competitorBrandList = map.get(brandId);
        if (CollectionUtil.isNotEmpty(competitorBrandList)) {
            for (UserBrandsContext userBrandsContext : competitorBrandList) {
                result.put(userBrandsContext.getBrandId(),userBrandsContext.getBrandName());
                List<Map> list = userBrandsContext.getCompetitor();
                for (Map map1 : list) {
                    Integer competitorBrandId = Integer.parseInt((String) map1.get("competitorBrandId"));
                    String competitorBrandName = (String) map1.get("competitorBrandName");
                    result.put(competitorBrandId,competitorBrandName);
                }
            }
        }
        return result;
    }

}
